import { get, post, put, del } from '@/utils/request'

// 资产相关接口
export const assetApi = {
  // 获取资产列表
  getList: (params: any) => get('/asset/list', params),
  // 获取资产详情
  getDetail: (id: number) => get(`/asset/${id}`),
  // 新增资产
  add: (data: any) => post('/asset', data),
  // 更新资产
  update: (id: number, data: any) => put(`/asset/${id}`, data),
  // 删除资产
  delete: (id: number) => del(`/asset/${id}`),
  // 批量删除资产
  batchDelete: (ids: number[]) => del('/asset/batch', { data: ids }),
  // 导入资产
  import: (file: File) => {
    const formData = new FormData()
    formData.append('file', file)
    return post('/asset/import', formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    })
  },
  // 导出资产
  export: (params: any) => get('/asset/export', params, { responseType: 'blob' })
}

// 分类相关接口
export const categoryApi = {
  // 获取分类列表
  getList: (params: any) => get('/category/list', params),
  // 获取分类树
  getTree: () => get('/category/tree'),
  // 获取分类详情
  getDetail: (id: number) => get(`/category/${id}`),
  // 新增分类
  add: (data: any) => post('/category', data),
  // 更新分类
  update: (id: number, data: any) => put(`/category/${id}`, data),
  // 删除分类
  delete: (id: number) => del(`/category/${id}`),
  // 批量删除分类
  batchDelete: (ids: number[]) => del('/category/batch', { data: ids }),
  // 获取全部分类（下拉）
  getAll: () => get('/category/all')
}

// 用户相关接口
export const userApi = {
  // 获取用户列表
  getList: (params: any) => get('/user/list', params),
  // 获取用户详情
  getDetail: (id: number) => get(`/user/${id}`),
  // 新增用户
  add: (data: any) => post('/user', data),
  // 更新用户
  update: (id: number, data: any) => put(`/user/${id}`, data),
  // 删除用户
  delete: (id: number) => del(`/user/${id}`),
  // 批量删除用户
  batchDelete: (ids: number[]) => del('/user/batch', { data: ids }),
  // 更新用户状态
  updateStatus: (id: number, status: number) => put(`/user/${id}/status`, null, { params: { status } }),
  // 重置密码
  resetPassword: (id: number) => put(`/user/${id}/password`),
  // 获取所有用户（用于下拉选择）
  getAll: () => get('/user/all')
}

// 角色相关接口
export const roleApi = {
  // 获取角色列表
  getList: (params: any) => get('/role/list', params),
  // 获取角色详情
  getDetail: (id: number) => get(`/role/${id}`),
  // 新增角色
  add: (data: any) => post('/role', data),
  // 更新角色
  update: (id: number, data: any) => put(`/role/${id}`, data),
  // 删除角色
  delete: (id: number) => del(`/role/${id}`),
  // 批量删除角色
  batchDelete: (ids: number[]) => del('/role/batch', { data: ids }),
  // 更新角色状态
  updateStatus: (id: number, status: number) => put(`/role/${id}/status`, null, { params: { status } }),
  // 获取所有角色（用于下拉选择）
  getAll: () => get('/role/all')
}

// 维护相关接口
export const maintenanceApi = {
  // 获取维护记录列表
  getList: (params: any) => get('/maintenance/list', params),
  // 获取维护记录详情
  getDetail: (id: number) => get(`/maintenance/${id}`),
  // 新增维护记录
  add: (data: any) => post('/maintenance', data),
  // 更新维护记录
  update: (id: number, data: any) => put(`/maintenance/${id}`, data),
  // 删除维护记录
  delete: (id: number) => del(`/maintenance/${id}`),
  // 批量删除维护记录
  batchDelete: (ids: number[]) => del('/maintenance/batch', { data: ids }),
  // 更新维护状态
  updateStatus: (id: number, status: number) => put(`/maintenance/${id}/status`, null, { params: { status } }),
  // 根据资产ID获取维护记录
  getByAssetId: (assetId: number) => get(`/maintenance/asset/${assetId}`),
  // 获取维护统计信息
  getStats: () => get('/maintenance/stats')
}

// 维护计划相关接口
export const maintenancePlanApi = {
  getList: (params: any) => get('/maintenance-plan/list', params),
  add: (data: any) => post('/maintenance-plan', data),
  update: (id: number, data: any) => put(`/maintenance-plan/${id}`, data),
  delete: (id: number) => del(`/maintenance-plan/${id}`),
  batchDelete: (ids: number[]) => del('/maintenance-plan/batch', { data: ids }),
  updateStatus: (id: number, status: number, progress: number) =>
    put(`/maintenance-plan/${id}/status`, null, { params: { status, progress } })
}

// 操作日志相关接口
export const operationLogApi = {
  // 获取操作日志列表
  getList: (params: any) => get('/operation-log/list', params),
  // 获取操作日志详情
  getDetail: (id: number) => get(`/operation-log/${id}`),
  // 删除操作日志
  delete: (id: number) => del(`/operation-log/${id}`),
  // 批量删除操作日志
  batchDelete: (ids: number[]) => del('/operation-log/batch', { data: ids }),
  // 清空操作日志
  clear: () => del('/operation-log/clear'),
  // 获取操作日志统计信息
  getStats: () => get('/operation-log/stats'),
  // 获取操作类型统计
  getOperationTypeStats: () => get('/operation-log/operation-type-stats'),
  // 获取模块统计
  getModuleStats: () => get('/operation-log/module-stats')
}

// 仪表盘相关接口
export const dashboardApi = {
  // 获取仪表盘数据
  getData: () => get('/dashboard/data'),
  // 获取资产统计
  getAssetStats: () => get('/dashboard/asset-stats'),
  // 获取维护统计
  getMaintenanceStats: () => get('/dashboard/maintenance-stats'),
  // 获取趋势数据
  getTrendData: (params: any) => get('/dashboard/trend', params)
}

// 资产历史相关接口（替代借用、转移、折旧等旧表）
export const historyApi = {
  // 根据资产ID获取历史记录
  getByAsset: (assetId: number) => get(`/asset/${assetId}/history`),
  // 新增历史记录
  add: (data: any) => post('/asset/history', data)
}

// 附件上传/下载统一接口
export const attachmentApi = {
  // 上传附件：bizType = 'asset' | 'maintenance'
  upload: (formData: FormData) =>
    post('/upload', formData, {
      headers: { 'Content-Type': 'multipart/form-data' }
    }),

  // 当前后端仅实现上传，下载/删除需后续实现
  download: (url: string) =>
    get(url, null, { responseType: 'blob' }),

  // 删除附件
  delete: (url: string) => del(url)
}

// 认证相关接口
export const authApi = {
  login: (username: string, password: string) =>
    post('/auth/login', null, {
      params: { username, password }
    })
} 